// pages/waterfall/waterfall.js

// page/search/index.js
var config = require('../../config.js');
var loadSearch = require('loadSearch.js');
var app = getApp();
var page = 0;
var size = 12;
var tab_new = {};//最新
var tab_hot = {};//最热
var tab_tj = {};//推荐
var tab_index = 0;
var top_list = [];//存放菜单
var top = {};

var tab_page = {};//热词对象
var inputVal = '';//表示input输入的值
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tab_list: [{
      id: 'view',
      name: '视图容器',
      open: false,
      pages: ['最新', '最热', '推荐']
    },
    ],
    list: [],
    list_display: 'block',//显示系列列表
    words_display: 'none',//显示热词,
    hotwords_list: [],//热词结果,
    btn_display: 'none',//取消按钮显示或不显示
    footerConfig: {
      Search: true
    },
    windowHeight: 0,
    lastLoadTime: 0,
    data_index:0,
    clickSeriesId:0//防止重复点击系列
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    top_list = [];
    that.setData({
      isShow:false
    });
    wx.getSystemInfo({
      success: function (res) {
        //赋值
        that.setData({
          windowHeight: res.windowHeight - 130
        });
      }
    });
    wx.showLoading({
      title: '加载中···',
    });
   
    var data = {
      tagName: config.tagName,
      page: 0,
      size: size,
      sort: 'time,desc'
    };
   // loadSearch.load_list(data, 0, tab_new, that);
    var timeout = setTimeout(function () {
      wx.hideLoading();
      clearTimeout(timeout);
    }, 500);
    
   
  },
  timescreen:function(){
    wx.navigateTo({
      url: '../waterfall/timelist',
      success: function (res) {

      },
      fail: function (res) {

      },
      complete: function (res) {

      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      isShow: true
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      searchKeyFlag: false
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }, loadMore: function () {//加载更多
    var that = this;
    if (this.data.list_dispaly == "none") {
      return;
    }
    var tab_list_l = [];
    if (tab_index == 1) {//最新
      var currentPage = tab_new.page + 1;
      if (currentPage < tab_new.pageSize) {

        var curTime = new Date().getTime();
        //上一次加载的时间
        var lastTime = this.data.lastLoadTime;
        that.setData({
          lastLoadTime: curTime
        });
        if (curTime - lastTime < 300) {
          return;
        }
        wx.showLoading({
          title: '加载中···',
        });
        var data = {
          tagName: config.tagName,
          page: currentPage,
          size: size,
          sort: 'time,desc'
        };
       
        loadSearch.load_page(data, currentPage, tab_new, that);
      }

    } else if (tab_index == 0) {//最热
      var currentPage = tab_hot.page + 1;
      if (currentPage < tab_hot.pageSize) {
        var curTime = new Date().getTime();
        //上一次加载的时间
        var lastTime = this.data.lastLoadTime;
        that.setData({
          lastLoadTime: curTime
        });
        if (curTime - lastTime < 300) {
          return;
        }
        wx.showLoading({
          title: '加载中···',
        });
        var data = {
          tagName: config.tagName,
          page: currentPage,
          size: size,
          sort: 'counter.viewingCount,desc'
        };
       
        loadSearch.load_page(data, currentPage, tab_hot, that);
      }
    } else {//推荐
      var currentPage = tab_tj.page + 1;
      if (currentPage < tab_tj.pageSize) {
        var curTime = new Date().getTime();
        //上一次加载的时间
        var lastTime = this.data.lastLoadTime;
        that.setData({
          lastLoadTime: curTime
        });
        if (curTime - lastTime < 300) {
          return;
        }
        wx.showLoading({
          title: '加载中···',
        });
        var data = {
          tagName: config.tagName,
          status: 'QUALIFIED',
          page: currentPage,
          size: size,
          sort: 'time,desc'
        };
       
        loadSearch.load_status_page(data, currentPage, tab_tj, that);
      }
    }
    var timeout = setTimeout(function () {
      wx.hideLoading();
      clearTimeout(timeout);
    }, 500);
  }, refesh: function () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: config.shareDes,
      path: '/pages/index/index',
      imageUrl: '../images/sharepic_1.jpg'
    }
  },
  bind_load: function (e) {//切换tab的时候，若是存在直接返回，否则加载
    var that = this
    var data_index = e.currentTarget.dataset.id;
    tab_index = data_index;

    that.setData({
      data_index:tab_index 
    });
    if (data_index == 1) {//表示是最热
      if (undefined == tab_hot.list) {//表示不存在，那么就加载
        var data = {
          tagName: config.tagName,
          page: page,
          size: size,
          sort: 'counter.viewingCount,desc'
        };
        loadSearch.load_list(data, page, tab_hot, that);
      }
      that.setData({
        list: tab_hot.list
      });

    } else if (data_index == 0) {//最新
      if (undefined == tab_new.list) {//表示不存在，那么就加载
        var data = {
          tagName: config.tagName,
          page: page,
          size: size,
          sort: 'time,desc'
        };

        loadSearch.load_list(data, page, tab_new, that);
      }
      that.setData({
        list: tab_new.list
      });
    }
    else {
     
    var data = {
       tagName: config.tagName,
       status: 'QUALIFIED',
       page: page,
       size: size,
       sort: 'time,desc'
     };
      loadSearch.load_status_list(data, page, tab_tj, that);
    }
  },
  focusSearch: function (e) {//聚焦的时候
    var keywords = e.detail.value;
    var that = this;
    inputVal = keywords;
    that.setData({
      btn_display: 'block',
      list_display: 'none',//显示系列列表
      words_display: 'block',//显示热词,
      keywords: keywords,
      isShow:keywords?false:true
    });
    if (!keywords) {
      return;
    }
    loadSearch.load_hotword(keywords, tab_page, that);
  }, confirmSearch: function (e) {//bindconfirm 完成的时候
    if (!inputVal) {
      return;
    }
    wx.navigateTo({
      url: '../searchlist/searchlist?keywords=' + inputVal,
    });
  },
  inputSearch: function (e) {//
    var that = this;
    var keywords = e.detail.value;
    inputVal = keywords;
    that.setData({
      btn_display: 'block',
      list_display: 'none',//显示系列列表
      words_display: 'block',//显示热词,
    });
    if (!keywords) {
      that.setData({
        hotwords_list: []
      });
      return;
    }
    loadSearch.load_hotword(keywords, tab_page, that);
  }, searchSubmit: function (e) {
    var keywords = e.detail.value;
    var keywords = e.detail.value.teamSearchKeyWords;  //if(keywords){
    if (!keywords) {
      that.setData({
        hotwords_list: []
      });
      return;
    }
    wx.navigateTo({
      url: '../searchlist/searchlist?keywords=' + keywords,
    });
  }, searchHot: function (e) {
    var keywords = e.currentTarget.dataset.keywords;
    wx.navigateTo({
      url: '../searchlist/searchlist?keywords=' + keywords,
    });
  },
  inplay_series: function (e) {
    var id = e.currentTarget.dataset.id;
    if(id==0||id==null){
      return;
    }
    if (this.data.clickSeriesId==id){
      return;
    }
    this.setData({
      clickSeriesId:id
    });
    wx.navigateTo({
      url: '../search/seriesList/serieslist?id=' + id,
    });
  }, cancelBtn: function () {
    // var that = this;
    // that.setData({
    //   btn_display: 'none',
    //   list_display: 'block',//显示系列列表
    //   words_display: 'none',//显示热词,
    // });
    wx.navigateBack({
      delta: 1
    })

  }, play: function (e) {
    wx.redirectTo({
      url: '../index/index',
    })
  }, search: function (e) {
    
  }, user: function (e) {
    wx.redirectTo({
      url: '../user/user',
    })
  }, kindToggle: function (e) {
    var id = e.currentTarget.id, list = this.data.tab_list;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    this.setData({
      tab_list: list
    });
  },
  sheNull:function(e){
    var keywords='';
    this.setData({
      keywords:'',
      isShow: keywords ? false : true,
      hotwords_list: []
    });
  },
  she:function(e){
    this.setData({
      isShow:false
    });
  }
})

  
 
